package com.cpone.cloudweb.utils.bak;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TranCharset {
	private static final String PRE_FIX_UTF = "&#x";
	private static final String POS_FIX_UTF = ";";

	public TranCharset() {
	}

	public static String XmlFormalize(String sTemp) {
		StringBuffer sb = new StringBuffer();

		if (sTemp == null || sTemp.equals("")) {
			return "";
		}

		String s = TranCharset.TranEncodeTOGB(sTemp);
		for (int i = 0; i < s.length(); i++) {
			char cChar = s.charAt(i);
			if (TranCharset.isGB2312(cChar)) {
				sb.append(PRE_FIX_UTF);
				sb.append(Integer.toHexString(cChar));
				sb.append(POS_FIX_UTF);
			} else {
				switch ((int) cChar) {
				case 32:
					sb.append("&#32;");
					break;
				case 34:
					sb.append("&quot;");
					break;
				case 38:
					sb.append("&amp;");
					break;
				case 60:
					sb.append("&lt;");
					break;
				case 62:
					sb.append("&gt;");
					break;
				default:
					sb.append(cChar);
				}
			}
		}
		return sb.toString();
	}

	/**
	 * ���ַ��������ʽת��GB2312
	 * 
	 * @param str
	 * @return
	 */
	public static String TranEncodeTOGB(String str) {
		try {
			String strEncode = TranCharset.getEncoding(str);
			String temp = new String(str.getBytes(strEncode), "GB2312");
			return temp;
		} catch (java.io.IOException ex) {
			return null;
		}
	}

	/**
	 * �ж������ַ��Ƿ�Ϊgb2312�ı����ʽ
	 * 
	 * @param c �����ַ�
	 * @return �����gb2312�����棬���򷵻ؼ�
	 */
	public static boolean isGB2312(char c) {
		Character ch = new Character(c);
		String sCh = ch.toString();
		try {
			byte[] bb = sCh.getBytes("gb2312");
			if (bb.length > 1) {
				return true;
			}
		} catch (java.io.UnsupportedEncodingException ex) {
			return false;
		}
		return false;
	}

	/**
	 * �ж��ַ����ı���
	 * 
	 * @param str
	 * @return
	 */
	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s = encode;
				return s;
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}

	public static Connection getConn() {
		
		String driver = "oracle.jdbc.driver.OracleDriver";
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.2.106:1521:project","plsuser","pls111111");
		} catch (Exception e) {
			// TODO: handle exception
		}
			
		return conn;
		
	}
	
	public static List query() {
		Statement stmt = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try {
			stmt = getConn().createStatement();
			rs = stmt.executeQuery("select id,autoResult from listen_write_info where id = 373604455");
			while(rs.next()){
				SecurityUtil des = new SecurityUtil();
				//System.out.println(getEncoding(rs.getString(2)));
				//System.out.println(rs.getString(2).trim().replace(" ", "") + "===");
				System.out.println(new String(replaceBlank(rs.getString(2)).getBytes("UTF-8"),"GB2312"));
				//byte[] ss = rs.getString(2).getBytes("utf-8");
				//byte[] ss2 = rs.getString(2).getBytes("iso8859-1");
				//System.out.println(rs.getString(2));
				//System.out.println(new String(ss,"iso8859-1"));
				/*System.out.println(new String(ss2,"GB18030"));*/
				
				list.add(rs.getString(2));
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
	
	public static String replaceBlank(String str) {
		if(!"".equals(str) && null!=str) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m =p.matcher(str);
			String s = "";
			s = m.replaceAll("");
			return s;
		} 
		return "";
	}
	
	public static void main(String[] args) {
		try {
			
			List l = query();
			Object s = l.get(0);
			String s1 = new String();
			String s2 = new String();
			String s3 = new String();
			
			
			
			System.out.println(s);
			/*
			System.out.println(s+"---");
			s1 = new String(s.getBytes("UTF-8"));
			System.out.println(s1);
			s2 = new String(s.getBytes("UTF-8"),"GB18030");
			System.out.println(s2);
			s3 = new String(s.getBytes("UTF-8"),"Unicode");
			System.out.println(s3);
			System.out.println(new String(s.getBytes(),"UTF-8"));
			System.out.println(new String(s.getBytes(),"GB2312"));
			System.out.println(new String(s.getBytes(),"ISO8859_1"));
			System.out.println(new String(s.getBytes("UTF-8"),"GB2312"));
			System.out.println(new String(s.getBytes("GB2312"),"UTF-8"));
			System.out.println(new String(s.getBytes("ISO8859_1"),"UTF-8"));
			System.out.println(new String(s.getBytes("UTF-8"),"ISO8859_1"));
			System.out.println(new String(s.getBytes("ISO8859_1"),"GB2312"));
			System.out.println(new String(s.getBytes("GB2312"),"ISO8859_1"));
			*/
			
			
			//System.out.println();
			
			/*
			String sd = new String("����".getBytes(), "UTF-8");
			System.out.println(getEncoding(sd));
			System.out.println(isGB2312(sd.toCharArray()[0]));
			
			System.out.println("****" + sd);
			
			System.out.println("****" + TranEncodeTOGB(sd));
			
			System.out.println(Charset.defaultCharset());
			
			System.out.println(XmlFormalize(sd));
			System.out.println(XmlFormalize("����"));*/
			/*
			System.out.println("����");// 1
			System.out.println("����".getBytes());// 2
			System.out.println("����".getBytes("GB2312"));// 3
			System.out.println("����".getBytes("ISO8859_1"));// 4
			
			System.out.println(new String("����".getBytes()));// 5
			System.out.println(new String("����".getBytes(), "UTF-8"));// 6
			System.out.println(new String("����".getBytes(), "ISO8859_1"));// 7

			System.out.println(new String("����".getBytes("GB2312")));// 8
			System.out.println(new String("����".getBytes("GB2312"), "GB2312"));// 9
			System.out.println(new

			String("����".getBytes("GB2312"), "ISO8859_1"));// 10

			System.out.println(new String("����".getBytes("ISO8859_1")));// 11
			System.out.println(new String("����".getBytes("ISO8859_1"), "GB2312"));// 12
			System.out.println(new String("����".getBytes("ISO8859_1"), "ISO8859_1"));// 13*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
